Trò chơi Angry Birds trong UNITY Engine
31.700 lượt xem;
- ColorTweenProperty.cs
- genericTypes /
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System;
3 using System.Collections;
4
5
6 public class ColorTweenProperty : AbstractColorTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9 private Action<Color> _setter;
10
11
12 public ColorTweenProperty( string propertyName, Color endValue, bool isRelative = false ) : base( endValue, isRelative )
13 {
14 this.propertyName = propertyName;
15 }
16
17
18 /// <summary>
19 /// validation checks to make sure the target has a valid property with an accessible setter
20 /// </summary>
21 public override bool validateTarget( object target )
22 {
23 // cache the setter
24 _setter = GoTweenUtils.setterForProperty<Action<Color>>( target, propertyName );
25 return _setter != null;
26 }
27
28
29 public override void prepareForUse()
30 {
31 // retrieve the getter
32 var getter = GoTweenUtils.getterForProperty<Func<Color>>( _ownerTween.target, propertyName );
33
34 _endValue = _originalEndValue;
35
36 // if this is a from tween we need to swap the start and end values
37 if( _ownerTween.isFrom )
38 {
39 _startValue = _endValue;
40 _endValue = getter();
41 }
42 else
43 {
44 _startValue = getter();
45 }
46
47 base.prepareForUse();
48 }
49
50
51 public override void tick( float totalElapsedTime )
52 {
53 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
54 var newColor = GoTweenUtils.unclampedColorLerp( _startValue, _diffValue, easedTime );
55
56 _setter( newColor );
57 }
58
59 }
2 using System;
3 using System.Collections;
4
5
6 public class ColorTweenProperty : AbstractColorTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9 private Action<Color> _setter;
10
11
12 public ColorTweenProperty( string propertyName, Color endValue, bool isRelative = false ) : base( endValue, isRelative )
13 {
14 this.propertyName = propertyName;
15 }
16
17
18 /// <summary>
19 /// validation checks to make sure the target has a valid property with an accessible setter
20 /// </summary>
21 public override bool validateTarget( object target )
22 {
23 // cache the setter
24 _setter = GoTweenUtils.setterForProperty<Action<Color>>( target, propertyName );
25 return _setter != null;
26 }
27
28
29 public override void prepareForUse()
30 {
31 // retrieve the getter
32 var getter = GoTweenUtils.getterForProperty<Func<Color>>( _ownerTween.target, propertyName );
33
34 _endValue = _originalEndValue;
35
36 // if this is a from tween we need to swap the start and end values
37 if( _ownerTween.isFrom )
38 {
39 _startValue = _endValue;
40 _endValue = getter();
41 }
42 else
43 {
44 _startValue = getter();
45 }
46
47 base.prepareForUse();
48 }
49
50
51 public override void tick( float totalElapsedTime )
52 {
53 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
54 var newColor = GoTweenUtils.unclampedColorLerp( _startValue, _diffValue, easedTime );
55
56 _setter( newColor );
57 }
58
59 }